﻿//$(function () {
//    $('.html5-color').each(function () { $(this).attr('type', 'color'); });
//});
//$(function () {
//    $('.html5-date').each(function () { $(this).attr('type', 'date'); });
//});
//$(function () {
//    $('.html5-datetime').each(function () { $(this).attr('type', 'datetime'); });
//});
//$(function () {
//    $('.html5-datetime-local').each(function () { $(this).attr('type', 'datetime-local'); });
//});
//$(function () {
//    $('.html5-email').each(function () { $(this).attr('type', 'email'); });
//});
//$(function () {
//    $('.html5-month').each(function () { $(this).attr('type', 'month'); });
//});
//$(function () {
//    $('.html5-number').each(function () { $(this).attr('type', 'number'); });
//});
//$(function () {
//    $('.html5-range').each(function () { $(this).attr('type', 'range'); });
//});
//$(function () {
//    $('.html5-search').each(function () { $(this).attr('type', 'search'); });
//});
//$(function () {
//    $('.html5-tel').each(function () { $(this).attr('type', 'tel'); });
//});
//$(function () {
//    $('.html5-time').each(function () { $(this).attr('type', 'time'); });
//});
//$(function () {
//    $('.html5-url').each(function () { $(this).attr('type', 'url'); });
//});
//$(function () {
//    $('.html5-week').each(function () { $(this).attr('type', 'week'); });
//});
//==========================================================================================================================

 //Example!!!!!!
//<input id="txtInput" type="text" OnKeyUp="CheckLength(this, lblMsg, 8);" autofocus><br>
//<label id="lblMsg"></label>

function CheckLength(idInput, idOutput, maxlength) {
    if (idInput != null && idInput.value.length <= maxlength) {
        idOutput.innerHTML = idInput.value.length + '/' + maxlength + ' ký tự. ';
    }
    else {
        idOutput.innerHTML = 'Cảnh báo: Bạn đã nhập quá ' + idInput.value.length + '/' + maxlength + ' ký tự. ';
    }
}
//==========================================================================================================================



$(function () {
    var domain = 'http://nguyenmanhthang.net';

    // Them target="_blank" cho tat ca cac link
    //$('a').attr('target', '_blank');                               //OK
    //$('a:not([href*="' + domain + '"])').attr('target', '_blank');   //OK

    // Them rel="nofollow" cho cac link den trang web khac (khong cung domain)
    //$('a:not([href^=' + domain + '])').attr('rel', 'nofollow');   //Error
    //$('a[href^="' + domain + '"]').attr('rel', 'nofollow');       // Ngược
    //$('a:not([href*="' + domain + '"])').attr('rel', 'nofollow');   //OK
});



if (($(window).height() + 100) < $(document).height()) {
    $('#top-link-block').removeClass('hidden').affix({
        // how far to scroll down before link "slides" into view
        offset: { top: 100 }
    });
}

//setTimeout(function () { callRegNewsLetter(); }, 30000);
//function callRegNewsLetter() {
//    $('#divRegPopup').modal('show');
//    $("#txtEmailReg").focus();
//}

function callPreviewFullScreen() {
    $('#divPreviewFullScreen').modal('show');
}

$(document).ready(function () {
    $("#btnRegNewsPopup").click(function () {
        $.ajax({
            type: "POST",
            url: "http://nguyenmanhthang.net/admin/ws_ajax.aspx/regnewsletter",
            data: "{'sEmail':'" + txtEmailPopup.value + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                $("#lblMsgRegNewsPopup").text(response.d);
            },
            error: function (r) {
                result = false;
                alert(r.d);
            }
        });
    });
});

$(document).ready(function () {
    $("#btnRegNews").click(function () {
        $.ajax({
            type: "POST",
            url: "http://nguyenmanhthang.net/admin/ws_ajax.aspx/regnewsletter",
            data: "{'sEmail':'" + txtEmail.value + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                $("#lblMsgRegNews").text(response.d);
            },
            error: function (r) {
                result = false;
                alert(r.d);
            }
        });
    });
});

function callUpdate() {
    $.ajax({
        type: "POST",
        url: "http://nguyenmanhthang.net/admin/ws_ajax.aspx/updatequantitydownload",
        data: "{'sEmail':'" + window.location.href + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            //alert(r.d);
        },
        error: function (r) {
            result = false;
            alert(r.d);
        }
    });
}


//==========================================================================================================================

function fncComment_Visit_Like(token) {
    $.ajax({
        type: "POST",
        url: "http://nguyenmanhthang.net/admin/ws_ajax.aspx/comment_visit_like",
        data: "{'token':'" + token + "','value':''}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            //alert(r.d);
        },
        error: function (r) {
            result = false;
            alert(r.d);
        }
    });
}


//==========================================================================================================================
function checkAll(idAd, idDiv) {
    var checkboxes = new Array();
    checkboxes = document.getElementById(idDiv).getElementsByTagName('INPUT');
    for (var i = 0; i < checkboxes.length; i++) {
        if (checkboxes[i].type == 'checkbox') {
            checkboxes[i].checked = idAd.checked;
        }
    }
}


function getValueRange(ValueRange, idShow) {
    idShow.innerHTML = ValueRange;
}


//==========================================================================================================================
$(document).ready(function () {
    var $lightbox = $('#lightbox');

    $('[data-target="#lightbox"]').on('click', function (event) {
        var $img = $(this).find('img'),
            src = $img.attr('src'),
            alt = $img.attr('alt'),
            css = {
                'maxWidth': $(window).width() - 100,
                'maxHeight': $(window).height() - 100
            };

        $lightbox.find('.close').addClass('hidden');
        $lightbox.find('img').attr('src', src);
        $lightbox.find('img').attr('alt', alt);
        $lightbox.find('img').css(css);
    });

    $lightbox.on('shown.bs.modal', function (e) {
        var $img = $lightbox.find('img');

        $lightbox.find('.modal-dialog').css({ 'width': $img.width() });
        $lightbox.find('.close').removeClass('hidden');
    });
});
//==========================================================================================================================

$(document).ready(function () {
    $('[data-toggle="tooltip"]').tooltip();
});

function GoogleSearch(keyCode, query) {
    if (keyCode == 13) {
        window.location.href = 'http://nguyenmanhthang.net/tim-kiem.html?q=' + query;
    }
    return false;
}







$(document).ready(function () {
    $('#list').click(function (event) { event.preventDefault(); $('#list').addClass('disabled'); $('#grid').removeClass('disabled'); $('#products .item').addClass('list-group-item'); $('#products .item').removeClass('grid-group-item'); });
    $('#grid').click(function (event) { event.preventDefault(); $('#list').removeClass('disabled'); $('#grid').addClass('disabled'); $('#products .item').removeClass('list-group-item'); $('#products .item').addClass('grid-group-item'); });
});

//$(document).ready(function () {
//    var url = $(location).attr('href');
//    if (url.length < 30)
//        $('#mnuDanhMuc').addClass('active');
//    else if (url.indexOf("danh-muc") != -1)
//        $('#mnuDanhMuc').addClass('active');
//    else if (url.indexOf("ung-dung-online") != -1)
//        $('#mnuUngDungOnline').addClass('active');
//    else if (url.indexOf("hoi-dap") != -1)
//        $('#mnuHoiDap').addClass('active');
//    else if (url.indexOf("lien-he") != -1)
//        $('#mnuLienHe').addClass('active');
//    else
//        $('#mnuTimKiem').addClass('active');
//});

$(document).ready(function () {
    if (window.location.pathname == '/' || window.location.pathname.toLowerCase().indexOf('/danh-muc') != -1 || window.location.pathname.toLowerCase().indexOf('/bai-viet') != -1 || window.location.pathname.toLowerCase().indexOf('/default.aspx') != -1 || window.location.pathname.toLowerCase().indexOf('/category.aspx') != -1 || window.location.pathname.toLowerCase().indexOf('/topic.aspx') != -1) {
        $('#mnuDanhMuc').addClass('active');
    }
    else if (window.location.pathname.toLowerCase().indexOf('/media.aspx') != -1 || window.location.pathname.toLowerCase().indexOf('/giai-tri') != -1) {
        $('#mnuGiaiTri').addClass('active');
    }
    else if (window.location.pathname.toLowerCase().indexOf('/contact.aspx') != -1 || window.location.pathname.toLowerCase().indexOf('/lien-he.html') != -1) {
        $('#mnuLienHe').addClass('active');
    }
    else if (window.location.pathname.toLowerCase().indexOf('/answer.aspx') != -1 || window.location.pathname.toLowerCase().indexOf('/question.aspx') != -1 || window.location.pathname.toLowerCase().indexOf('/hoi-dap') != -1) {
        $('#mnuHoiDap').addClass('active');
    }
    else if (window.location.pathname.toLowerCase().indexOf('/commonform.aspx') != -1 || window.location.pathname.toLowerCase().indexOf('/tim-kiem.html') != -1) {
        $('#mnuTimKiem').addClass('active');
    }
    else {
        $('#mnuUngDungOnline').addClass('active');
    }
});




//================================================== BEGIN TREE ========================================================
$.fn.extend({
    treed: function (o) {

        var openedClass = 'glyphicon-minus-sign';
        var closedClass = 'glyphicon-plus-sign';

        if (typeof o != 'undefined') {
            if (typeof o.openedClass != 'undefined') {
                openedClass = o.openedClass;
            }
            if (typeof o.closedClass != 'undefined') {
                closedClass = o.closedClass;
            }
        };

        //initialize each of the top levels
        var tree = $(this);
        tree.addClass("tree");
        tree.find('li').has("ul").each(function () {
            var branch = $(this); //li with children ul
            branch.prepend("<i class='indicator glyphicon " + closedClass + "'></i>");
            branch.addClass('branch');
            branch.on('click', function (e) {
                if (this == e.target) {
                    var icon = $(this).children('i:first');
                    icon.toggleClass(openedClass + " " + closedClass);
                    $(this).children().children().toggle();
                }
            })
            branch.children().children().toggle();
        });
        //fire event from the dynamically added icon
        tree.find('.branch .indicator').each(function () {
            $(this).on('click', function () {
                $(this).closest('li').click();
            });
        });
        //fire event to open branch if the li contains an anchor instead of text
        tree.find('.branch>a').each(function () {
            $(this).on('click', function (e) {
                $(this).closest('li').click();
                e.preventDefault();
            });
        });
        //fire event to open branch if the li contains a button instead of text
        tree.find('.branch>button').each(function () {
            $(this).on('click', function (e) {
                $(this).closest('li').click();
                e.preventDefault();
            });
        });
    }
});

//Initialization of treeviews
$('#tree_menu').treed();

$('#tree1').treed();

$('#tree2').treed({ openedClass: 'glyphicon-folder-open', closedClass: 'glyphicon-folder-close' });

$('#tree3').treed({ openedClass: 'glyphicon-chevron-right', closedClass: 'glyphicon-chevron-down' });
//================================================== END TREE ========================================================

//================================================== BEGIN SyntaxHighlighter ========================================================
$(document).ready(function () {
    SyntaxHighlighter.autoloader(
		['applescript', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushAppleScript.js'],
		['actionscript3', 'as3', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushAS3.js'],
		['bash', 'shell', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushBash.js'],
		['coldfusion', 'cf', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushColdFusion.js'],
		['cpp', 'c', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushCpp.js'],
		['c#', 'c-sharp', 'csharp', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushCSharp.js'],
		['css', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushCss.js'],
		['delphi', 'pascal', 'pas', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushDelphi.js'],
		['diff', 'patch', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushDiff.js'],
		['erl', 'erlang', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushErlang.js'],
		['groovy', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushGroovy.js'],
		['java', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushJava.js'],
		['jfx', 'javafx', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushJavaFX.js'],
		['js', 'jscript', 'javascript', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushJScript.js'],
		['perl', 'Perl', 'pl', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushPerl.js'],
		['php', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushPhp.js'],
		['text', 'plain', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushPlain.js'],
		['powershell', 'ps', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushPowerShell.js'],
		['py', 'python', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushPython.js'],
		['ruby', 'rails', 'ror', 'rb', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushRuby.js'],
		['sass', 'scss', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushSass.js'],
		['scala', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushScala.js'],
		['sql', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushSql.js'],
		['vb', 'vbnet', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushVb.js'],
		['xml', 'xhtml', 'xslt', 'html', 'http://nguyenmanhthang.net/ckeditor/plugins/syntaxhighlight/scripts/shBrushXml.js']
    );
    SyntaxHighlighter.defaults.toolbar = false;
    SyntaxHighlighter.all();
});
//================================================== END SyntaxHighlighter ========================================================



//$('.vddd input[type=checkbox]').each(function () {
    //$(this).attr('data-toggle', 'toggle');
    //$(this).attr('id', 'toggle--like');
    //$(this).attr('class', 'cmn-toggle cmn-toggle-round');
//});
$(document).ready(function () {
    fnReLoadUI();
});


function formatState(state) {
    if (!state.id) { return state.text; }
    var $state = $(
'<span><img src="vendor/images/flags/' + state.element.value.toLowerCase() + '.png" class="img-flag" /> ' + state.text + '</span>'
);
    return $state;
};

// Gọi lại hàm fnReLoadUI khi postback
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () { fnReLoadUI(); });
function fnReLoadUI() {
    $('.aspNetDisabled').addClass('form-control');

    //== Thêm class cho các control để có giao diện đẹp
    $('.divListCheckBox li').each(function () {
        $(this).attr('class', 'checkbox checkbox-success');
    });
    $('.divItemCheckBox input[type=checkbox]').each(function () {
        $(this).attr('class', 'cmn-toggle cmn-toggle-round');
    });
    $('.divListRadio li').each(function () {
        $(this).attr('class', 'radio radio-success radio-inline');
    });

    //== Thêm thuộc tính type theo Html5 (fix cho các bản nhỏ hơn .NET 3.5)
    $('input').each(function () {
        $(this).attr('type', $(this).attr('Html5'));
    });
    $('textarea').each(function () {
        $(this).attr('type', $(this).attr('Html5'));
    });

    //== Thêm nhiều thẻ form cho ASPX
    $('FormEX').replaceWith(function () {
        return $("<form />", { html: $(this).html() });
    });

    //== Thêm thuộc tính: rel, target cho đường link quảng cáo
    $('#divAdv a').attr('rel', 'nofollow');
    $('#divAdv a').attr('target', '_blank');

    //== BEGIN thêm target=_blank vào các link bên ngoài
    $('a[href*="http://"]:not([href*="http://nguyenmanhthang.net"])').attr('target', '_blank');
    $("a[href^='http://']").each(
        function(){
            if(this.href.indexOf(location.hostname) == -1) {
                $(this).attr('target', '_blank');
            }
        }
    );
    $("a[href^='https://']").each(
        function(){
            if(this.href.indexOf(location.hostname) == -1) {
                $(this).attr('target', '_blank');
            }
        }
    );
    //== END thêm target=_blank vào các link bên ngoài

    //== BEGIN active tab bootstrap when postback
    var _hfNameTabActive = $("[id*=hfNameTabActive]").val() != "" ? $("[id*=hfNameTabActive]").val() : "tabDefault";
    $('#Tabs a[href="#' + _hfNameTabActive + '"]').tab('show');
    $("#Tabs a").click(function () {
        $("[id*=hfNameTabActive]").val($(this).attr("href").replace("#", ""));
    });
    //== END active tab bootstrap when postback

    //== BEGIN sử dụng select2.github.io cho combobox và listbox
    //1. Single select boxes
    $(".js-example-basic-single").select2();

    //2. Multiple select boxes
    $(".js-example-basic-multiple").select2();

    //3. Placeholders
    $(".js-example-placeholder-single").select2({
        placeholder: "Select a state",
        allowClear: true
    });
    $(".js-example-placeholder-multiple").select2({
        placeholder: "Select a state"
    });

    //4. Limiting the number of selections
    $(".js-example-basic-multiple-limit").select2({
        maximumSelectionLength: 2
    })

    //5. Hiding the search box
    $(".js-example-basic-hide-search").select2({
        minimumResultsForSearch: Infinity
    });

    //6. Tagging support
    $(".js-example-tags").select2({
        tags: true
    })

    //7. Multiple languages
    $(".js-example-language").select2({
        language: "vi"
    });

    //8. RTL support
    $(".js-example-rtl").select2({
        dir: "rtl"
    });

    //9. Theme support
    $(".js-example-theme-single").select2({
        theme: "classic"
    });
    $(".js-example-theme-multiple").select2({
        theme: "classic"
    });

    //10. Templating
    $(".js-example-templating").select2({
        templateResult: formatState
    });
    //== END sử dụng select2.github.io cho combobox và listbox
}


//== BEGIN sau khoảng thời gian tự gọi lại để khỏi session timeout
function heartBeat() {
    $.get("../KeepAlive.ashx?", function (data) { });
}
$(function () {
    setInterval("heartBeat()", 1000 * 60); // 60s gửi request một lần
});
//== END sau khoảng thời gian tự gọi lại để khỏi session timeout

//== BEGIN sau khoảng thời gian tự gọi lại để khỏi session timeout
//function heartBeat() {
//    $.get("../KeepAlive.ashx?", function (data) { });
//}
//$(function () {
//    setInterval("heartBeat()", 1000 * 60); // 60s gửi request một lần
//});
$(function () {
    var iRF = Math.floor((Math.random() * 1000000) + 200000);
    setTimeout(fnAutoRefresh, iRF)
});
function fnAutoRefresh() {
    location.reload(false);
}
//== END sau khoảng thời gian tự gọi lại để khỏi session timeout

//== BEGIN config Facebook.Com
(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.3&appId=432781806807255";
    fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));
//== END config Facebook

//== BEGIN config TopDev.Vn
if (ATDOptions1 != undefined) {
    ATDOptions1.opt.client_id = '3e0063a96f23679fe2b07b8ef21bcecb';
    ATDOptions1.opt.widget_width = 500;
    ATDOptions1.opt.widget_height = 450;
    ATDOptions1.opt.type = ['WIDGET'];
} else {
    var ATDOptions1 = {};
    ATDOptions1.opt = { client_id: '3e0063a96f23679fe2b07b8ef21bcecb', widget_width: 500, widget_height: 450, type: ['WIDGET'] };
}
//== END config TopDev.Vn